﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ActivitiesControl.GUI.Silverlight.Services;
using System.ComponentModel;
using ActivitiesControl.GUI.Silverlight.Utils;
using ActivitiesControl.GUI.Silverlight.RemoteServices;

namespace ActivitiesControl.GUI.Silverlight.Views
{
	public partial class ChangePassword : ChildWindow
	{
		private ActivitiesControlServicesClient Service { get; set; }

		public ChangePassword()
		{
			InitializeComponent();
			Service = ServiceLocator.Instance.Service;
		}

		private void OKButton_Click(object sender, RoutedEventArgs e)
		{
			Service.UpdateUserPasswordCompleted -= new EventHandler<AsyncCompletedEventArgs>(Service_UpdateUserPasswordCompleted);
			Service.UpdateUserPasswordCompleted += new EventHandler<AsyncCompletedEventArgs>(Service_UpdateUserPasswordCompleted);

			Service.UpdateUserPasswordAsync(
				LoggedUser.Instance.User.Username,
				CurrentPasswordBox.Password,
				NewPasswordBox.Password,
				ConfirmNewPasswordBox.Password);
		}

		void Service_UpdateUserPasswordCompleted(object sender, AsyncCompletedEventArgs e)
		{
			if (e.Error == null)
			{
				Message.Show("Senha atualizada.");
				MessageTextBlock.Text = "Senha atualizada";
				this.DialogResult = true;
			}
			else
			{
				Message.Show("A senha não pôde ser alterada.");
				MessageTextBlock.Text = "A senha não pôde ser alterada.";
			}
		}

		private void CancelButton_Click(object sender, RoutedEventArgs e)
		{
			this.DialogResult = false;
			this.Close();
		}
	}
}

